package com.servlet;

import com.Imp.CartMapperImpl;
import com.Imp.CuisineMapperImpl;
import com.Imp.OManagementMapperImpl;
import com.Imp.OrderMapperImpl;
import com.entity.Cart;
import com.entity.OManagement;
import com.entity.Order;
import com.mapper.CartMapper;
import com.mapper.CuisineMapper;
import com.mapper.OManagementMapper;
import com.mapper.OrderMapper;
import com.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

public class OrderController extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CuisineMapper cuisineMapper = new CuisineMapperImpl();
        CartMapper cartMapper = new CartMapperImpl();
        OrderMapper orderMapper= new OrderMapperImpl();
        OManagementMapper oManagementMapper = new OManagementMapperImpl();
        String[] selectedItems = request.getParameterValues("selectedItems");
        String storeId = request.getParameter("storeId");
        HttpSession session = request.getSession();
        int orderId = generateOrderId();
        Date orderTime = getCurrentTime();
        String orderState = "待分配";
        double totalPrice = 0.0;
        String customerId = (String) session.getAttribute("username");
        String deliverymanId = null;
        if (selectedItems != null) {
            for (String cuisineId : selectedItems) {
                Cart cart = cartMapper.getCartByCuisineId(storeId,customerId,cuisineId);
                Order order = new Order(orderId,cuisineId,cart.getCuisineName(),cart.getPrice(),cart.getAmount());
                totalPrice+= cart.getPrice()*cart.getAmount();
                orderMapper.insertOrder(order);
            }
        }
        cartMapper.deleteByStoreIdAndCustomerId(storeId,customerId);
        OManagement oManagement = new OManagement(orderId,orderTime,orderState,totalPrice,storeId,customerId,deliverymanId);
        oManagementMapper.addOrder(oManagement);
        // 跳转到结算页面
        response.sendRedirect("waitingOrders.jsp");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String op=request.getParameter("op");
        if(op.equals("delete")){
            int orderId= Integer.parseInt(request.getParameter("orderId"));
            OrderMapper orderMapper = new OrderMapperImpl();
            OManagementMapper oManagementMapper = new OManagementMapperImpl();
            orderMapper.deleteOrder(orderId);
            oManagementMapper.deleteOrder(orderId);

            // 设置请求参数
            String redirectUrl = "/waitingOrders.jsp";
            // 重定向到指定页面
            response.sendRedirect(redirectUrl);
        }
    }
    // 生成随机订单号
    private int generateOrderId() {
        // 这里仅作示例，实际应根据业务需求生成唯一的订单号
        return (int) (Math.random() * 1000000);
    }
    // 获取当前时间
    private Date getCurrentTime() {
        return new Date();
    }

}
